/* 广度优先遍历 */
const tree = {
  val: "a",
  children: [
    {
      val: "b",
      children: [
        {
          val: "e",
          children: [],
        },
        {
          val: "f",
          children: [],
        },
      ],
    },
    {
      val: "c",
      children: [
        {
          val: "g",
          children: [],
        },
        {
          val: "h",
          children: [],
        },
      ],
    },
  ],
};

/**
 * 每次访问队首元素, 并且将对应的子节点存放到队列中
 * @param {*} root 
 */
const bfs = (root) => {
  const stack = [root];

  while (stack.length) {
    const n = stack.shift();
    console.log(n.val);
    n.children.forEach((child) => {
      stack.push(child);
    });
  }
};

bfs(tree);
